cpp: add insecure randomness query with RNG security models - #22437
cpp: add insecure randomness query with RNG security models#22437kumarak wants to merge 1 commit into
Conversation
Add the cpp/insecure-randomness query (CWE-330/338) that flags cryptographically insecure random numbers used as security-sensitive values (keys, IVs, nonces). - shared quantum: add isCryptographicallySecure() to Crypto::RandomNumberGenerationInstance (defaults to none). - cpp quantum lib: model C stdlib, POSIX/BSD, Windows CNG, and C++ <random> generators via the new randomNumberGeneratorModel extensible predicate; classify OpenSSL RAND_bytes/RAND_priv_bytes as secure and RAND_pseudo_bytes as insecure. - MaD: populate randomNumberGeneratorModel with the generator rows.
0c867c5 to
acb1923
Compare
There was a problem hiding this comment.
Pull request overview
Adds a C++ insecure-randomness query using shared cryptography models to track weak RNG output into keys, IVs, and nonces.
Changes:
- Adds cryptographic-security classification for RNG models.
- Models standard, platform, C++, and OpenSSL generators.
- Adds the query, documentation, change notes, and tests.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
shared/quantum/codeql/quantum/experimental/Model.qll |
Adds RNG security classification. |
shared/quantum/change-notes/2026-08-26-random-security-classification.md |
Documents the shared API. |
cpp/ql/test/query-tests/Security/CWE/CWE-330/test.cpp |
Exercises RNG sources and sinks. |
cpp/ql/test/query-tests/Security/CWE/CWE-330/options |
Configures test stubs. |
cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.qlref |
Registers the query test. |
cpp/ql/test/query-tests/Security/CWE/CWE-330/InsecureRandomness.expected |
Records expected results. |
cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.ql |
Implements taint tracking. |
cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.qhelp |
Documents the query. |
cpp/ql/src/Security/CWE/CWE-330/InsecureRandomness.c |
Provides usage examples. |
cpp/ql/src/change-notes/2026-08-26-insecure-randomness-query.md |
Announces the query. |
cpp/ql/lib/ext/experimental.quantum.Random.model.yml |
Defines RNG model rows. |
cpp/ql/lib/experimental/quantum/Standard/Random.qll |
Implements data-driven RNG models. |
cpp/ql/lib/experimental/quantum/OpenSSL/Random.qll |
Classifies OpenSSL RNGs. |
cpp/ql/lib/experimental/quantum/Language.qll |
Exposes standard RNG models. |
cpp/ql/lib/change-notes/2026-08-26-insecure-randomness-model.md |
Documents model additions. |
Suppressed comments (1)
cpp/ql/lib/ext/experimental.quantum.Random.model.yml:24
- In Windows headers,
RtlGenRandomis a macro alias forSystemFunction036, so preprocessing makes the call target's function nameSystemFunction036. The directRtlGenRandomdeclaration in this test masks that behavior, and this row will not model normal uses through the Windows API headers. Model the exported name as well and make the regression fixture use the real alias shape.
- ["", "", "RtlGenRandom", "0", true]
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| sink = any(Crypto::KeyGenerationOperationInstance op).getKeyValueConsumer() | ||
| } | ||
|
|
||
| predicate isBarrierIn(DataFlow::Node node) { isSource(node) } |
| // A global or `std` free function, e.g. `rand` or `std::rand`. | ||
| type = "" and | ||
| f.hasGlobalOrStdName(name) and | ||
| generatorName = name | ||
| or | ||
| // A member function of a class (template), e.g. `std::mt19937::operator()`. | ||
| type != "" and | ||
| f.getName() = name and | ||
| f.getDeclaringType().getSimpleName() = type and | ||
| (if namespace = "" then generatorName = type else generatorName = namespace + "::" + type) |
| - ["std", "mersenne_twister_engine", "operator()", "", false] | ||
| - ["std", "linear_congruential_engine", "operator()", "", false] | ||
| - ["std", "subtract_with_carry_engine", "operator()", "", false] | ||
| - ["std", "discard_block_engine", "operator()", "", false] |
| - ["", "", "jrand48", "", false] | ||
| - ["", "", "rand_r", "", false] | ||
| # POSIX/BSD generators returning the value (secure). | ||
| - ["", "", "arc4random", "", true] |
| { | ||
| OpenSslRandomNumberGeneratorInstance() { | ||
| this.(Call).getTarget().getName() in ["RAND_bytes", "RAND_pseudo_bytes"] | ||
| this.(Call).getTarget().getName() in ["RAND_bytes", "RAND_priv_bytes", "RAND_pseudo_bytes"] |
| - ["", "", "arc4random_uniform", "", true] | ||
| # Generators writing to a buffer argument (secure). | ||
| - ["", "", "arc4random_buf", "0", true] | ||
| - ["", "", "getrandom", "0", true] |
jketema
left a comment
There was a problem hiding this comment.
I see you turned this into draft after opening this PR. I just want to note that we will not accept any queries in our main line query directory that depend on query libraries from experimental directories.
Thanks, @jketema, for letting me know. I will remove the use of libraries from experimental directories. |
Add the cpp/insecure-randomness query (CWE-330/338) that flags cryptographically insecure random numbers used as security-sensitive values (keys, IVs, nonces).